/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By 深圳中科鑫智科技有限公司
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.app.service.product.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cereshop.app.dao.product.CereProductSkuDAO;
import com.shop.cereshop.app.page.cart.CartSku;
import com.shop.cereshop.app.page.product.*;
import com.shop.cereshop.app.param.groupwork.GroupWorkSettlementParam;
import com.shop.cereshop.app.param.settlement.ProductSku;
import com.shop.cereshop.app.service.product.CereProductSkuService;
import com.shop.cereshop.commons.domain.product.*;
import com.shop.cereshop.commons.domain.product.bo.CereProductSkuBo;
import com.shop.cereshop.commons.domain.product.bo.CereSkuNameBo;
import com.shop.cereshop.commons.domain.product.vo.CereProductSkuVo;
import com.shop.cereshop.commons.domain.product.vo.CereSkuNameVo;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.utils.EmptyUtils;
import org.assertj.core.util.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CereProductSkuServiceImpl extends ServiceImpl<CereProductSkuDAO,CereProductSku> implements CereProductSkuService {

    @Autowired
    private CereProductSkuDAO cereProductSkuDAO;



    /**
     * 条件构造器
     */
    public LambdaQueryWrapper<CereProductSku> buildLambdaQueryWrapper(CereProductSkuBo param){
        LambdaQueryWrapper<CereProductSku> q1 = new LambdaQueryWrapper<>();
        q1.eq(ObjectUtil.isNotEmpty(param.getSkuId()),CereProductSku::getSkuId,param.getSkuId());
        q1.eq(ObjectUtil.isNotEmpty(param.getProductId()),CereProductSku::getProductId,param.getProductId());
        q1.in(CollectionUtil.isNotEmpty(param.getProductIds()),CereProductSku::getSkuId,param.getProductIds());
        return q1;
    }

    /**
     * 条件查询
     */
    public List<CereProductSkuVo> queryList(CereProductSkuBo param){
        List<CereProductSku> list = list(buildLambdaQueryWrapper(param));

        if (CollectionUtil.isNotEmpty(list)){
            return generate(list);
        }
        return null;
    }
    /**
     * 根据规格id查询sku参数
     */
    public List<CereProductSkuVo> queryListByProductId(Long productId){
        CereProductSkuBo bo = new CereProductSkuBo();
        bo.setProductId(productId);
        return queryList(bo);
    }

    @Override
    public List<CereProductSkuVo> queryListByProductId(List<Long> productIds) {
        CereProductSkuBo bo = new CereProductSkuBo();
        bo.setProductIds(productIds);
        return queryList(bo);
    }

    /**
     * 转换函数
     */
    public List<CereProductSkuVo> generate(List<CereProductSku> list){
        List<CereProductSkuVo> cereProductSkuVos = Lists.newArrayList();
        for (CereProductSku cereProductSku : list) {
            CereProductSkuVo cereProductSkuVo = new CereProductSkuVo();
            BeanUtils.copyProperties(cereProductSku,cereProductSkuVo);
            cereProductSkuVos.add(cereProductSkuVo);
        }
        return cereProductSkuVos;
    }

    /**
     * 转换函数
     */
    public CereProductSkuVo generate(CereProductSku cereProductSku) {
        CereProductSkuVo cereProductSkuVo = new CereProductSkuVo();
        BeanUtils.copyProperties(cereProductSku,cereProductSkuVo);
        return cereProductSkuVo;
    }


    @Override
    public List<SkuName> findSkuNames(Long productId) {
        return cereProductSkuDAO.findSkuNames(productId);
    }

    @Override
    public List<SkuValue> findSkuValues(String skuName, Long productId) {
        return cereProductSkuDAO.findSkuValues(skuName,productId);
    }

    @Override
    public ProductSkus findSku(Long skuId) {
        return cereProductSkuDAO.findSku(skuId);
    }

    @Override
    public ProductSkus findFirstSku(Long productId) {
        return cereProductSkuDAO.findFirstSku(productId);
    }

    @Override
    public List<Sku> findSimpleSkuByProductId(Long productId) {
        return cereProductSkuDAO.findSimpleSkuByProductId(productId);
    }

    @Override
    public List<Sku> findSkuByProductId(Long productId) {
        return cereProductSkuDAO.findSkuByProductId(productId);
    }

    @Override
    public String findValueBySkuId(Long skuId) {
        return cereProductSkuDAO.findValueBySkuId(skuId);
    }

    @Override
    public Integer findStockNumberBySkuId(Long skuId) {
        return cereProductSkuDAO.findStockNumberBySkuId(skuId);
    }

    @Override
    public Integer findStockNumber(Long skuId) {
        return cereProductSkuDAO.findStockNumber(skuId);
    }

    @Override
    public List<CartSku> findStockNumberBySkus(List<ProductSku> skus) {
        return cereProductSkuDAO.findStockNumberBySkus(skus);
    }

    @Override
    public void updateBatch(List<CartSku> productSkus) throws CoBusinessException {
        cereProductSkuDAO.updateBatch(productSkus);
    }

    @Override
    public List<CartSku> findSkuBySkus(List<ProductSku> skus) {
        return cereProductSkuDAO.findSkuBySkus(skus);
    }

    @Override
    public List<CartSku> findSkuByOrderId(Long orderId) {
        return cereProductSkuDAO.findSkuByOrderId(orderId);
    }

    @Override
    public List<CartSku> findSkuByIds(List<Long> ids,Long orderId) {
        return cereProductSkuDAO.findSkuByIds(ids,orderId);
    }

    @Override
    public List<CereProductSku> findStockNumberByOrderId(Long orderId) {
        return cereProductSkuDAO.findStockNumberByOrderId(orderId);
    }

    @Override
    public List<CereProductSku> findAll() {
        return cereProductSkuDAO.findAll();
    }

    @Override
    public List<SkuValue> findOneValues(Long skuId) {
        return cereProductSkuDAO.findOneValues(skuId);
    }

    @Override
    public List<Sku> findGroupWorkSkuByProductId(Long productId, Long shopGroupWorkId) {
        return cereProductSkuDAO.findGroupWorkSkuByProductId(productId,shopGroupWorkId);
    }

    @Override
    public List<CartSku> findSkuBySkuId(Long skuId) {
        return cereProductSkuDAO.findSkuBySkuId(skuId);
    }

    @Override
    public List<Sku> findSeckillSkuByProductId(Long productId, Long shopSeckillId) {
        return cereProductSkuDAO.findSeckillSkuByProductId(productId,shopSeckillId);
    }

    @Override
    public List<Sku> findDiscountSkuByProductId(Long productId, Long shopDiscountId) {
        return cereProductSkuDAO.findDiscountSkuByProductId(productId,shopDiscountId);
    }

    @Override
    public List<CartSku> findSkuBySkuIdAndWorkId(GroupWorkSettlementParam param) {
        return cereProductSkuDAO.findSkuBySkuIdAndWorkId(param);
    }

    @Override
    public List<SkuValue> findValuesByProductId(Long productId) {
        return cereProductSkuDAO.findValuesByProductId(productId);
    }

    @Override
    public List<CartSku> findOriginalSkuBySkus(List<ProductSku> skus) {
        return cereProductSkuDAO.findOriginalSkuBySkus(skus);
    }

    @Override
    public List<CartSku> findGroupWorkStockNumberBySkus(List<ProductSku> skus,Long shopGroupWorkId) {
        return cereProductSkuDAO.findGroupWorkStockNumberBySkus(skus,shopGroupWorkId);
    }

    @Override
    public List<CartSku> findSeckillStockNumberBySkus(List<ProductSku> skus,Long shopSeckillId) {
        return cereProductSkuDAO.findSeckillStockNumberBySkus(skus,shopSeckillId);
    }

    @Override
    public List<CartSku> findDiscountStockNumberBySkus(List<ProductSku> skus,Long shopDiscountId) {
        return cereProductSkuDAO.findDiscountStockNumberBySkus(skus,shopDiscountId);
    }

    @Override
    public void updateBatchSkus(List<CereProductSku> productSkus) throws CoBusinessException {
        cereProductSkuDAO.updateBatchSkus(productSkus);
    }

    @Override
    public SkuTool findTool(Long productId) {
        return cereProductSkuDAO.findTool(productId);
    }

    @Override
    public ProductStockInfo findProductStockInfo(Long productId) {
        return cereProductSkuDAO.findProductStockInfo(productId);
    }

    @Override
    public int updateStockNumber(Long skuId, int stockNumber) {
        CereProductSku sku = new CereProductSku();
        sku.setSkuId(skuId);
        sku.setStockNumber(stockNumber);
        return cereProductSkuDAO.updateByPrimaryKeySelective(sku);
    }

    @Override
    public int rollbackStock(Long skuId, Integer buyNumber) {
        return cereProductSkuDAO.rollbackStock(skuId, buyNumber);
    }

}
